﻿/**
 * @file		timestamp.c
 * @brief		Timestamp Process
 * @note		None
 * @attention	None
 * 
 * <B><I>Copyright 2015 Socionext Inc.</I></B>
 */

/*----------------------------------------------------------------------*/
/* Definition															*/
/*----------------------------------------------------------------------*/
#define TIMESTAMP_CNTCVL		0x1D010008
#define TIMESTAMP_CNTCVU		0x1D01000C
#define TIMESTAMP_FREQUENCY		0x02625A00

/*----------------------------------------------------------------------*/
/* Enumeration															*/
/*----------------------------------------------------------------------*/
/* Nothing Special */

/*----------------------------------------------------------------------*/
/* Structure															*/
/*----------------------------------------------------------------------*/
/* Nothing Special */

/*----------------------------------------------------------------------*/
/* Global Data															*/
/*----------------------------------------------------------------------*/
/* Nothing Special */

/*----------------------------------------------------------------------*/
/* Macro														 		*/
/*----------------------------------------------------------------------*/
/* Nothing Special */

/*----------------------------------------------------------------------*/
/* Local Function														*/
/*----------------------------------------------------------------------*/
/* Nothing Special */

/*----------------------------------------------------------------------*/
/* Global Function														*/
/*----------------------------------------------------------------------*/
/**
 * start measure of time
 * @return Time[usec]
 */
unsigned long Timestamp_Get_us(void)
{
	unsigned long long	timestamp_counter;
	unsigned long		up_counter1 = 0;
	unsigned long		up_counter2 = 0;
	unsigned long		low_counter = 0;
	
	up_counter1 = *((volatile unsigned long*)TIMESTAMP_CNTCVU);
	low_counter = *((volatile unsigned long*)TIMESTAMP_CNTCVL);
	up_counter2 = *((volatile unsigned long*)TIMESTAMP_CNTCVU);
	while( up_counter1 != up_counter2 ){
		up_counter1 = up_counter2;
		low_counter = *((volatile unsigned long*)TIMESTAMP_CNTCVL);
		up_counter2 = *((volatile unsigned long*)TIMESTAMP_CNTCVU);
	}
	timestamp_counter = ((unsigned long long)up_counter2 << 32) + low_counter;
	
	return (unsigned long)(timestamp_counter / (TIMESTAMP_FREQUENCY / 1000000));
}
